package org.dromara.learning_xq.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.validation.Valid;
import org.dromara.learning_xq.entity.AsKnowledgeMap;
import org.dromara.learning_xq.entity.dto.AsKnowledgeMapDto;
import org.dromara.learning_xq.mapper.AsKnowledgeMapper;
import org.dromara.learning_xq.service.AsKnowledgeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class AsKnowledgeServiceimpl extends ServiceImpl<AsKnowledgeMapper, AsKnowledgeMap> implements AsKnowledgeService {


    @Lazy
    @Autowired
    private AsKnowledgeService asKnowledgeService;

    /**
     * 增加知识地图条目并返回所有条目的DTO列表
     *
     * @param asKnowledgeMapDto 要增加的知识地图条目的数据传输对象
     * @return 包含所有知识地图条目的DTO列表
     */
    @Override
    public List<AsKnowledgeMapDto> increaseAsKnowledgeMap(@Valid AsKnowledgeMapDto asKnowledgeMapDto) {
        // 验证数据有效性
        validateData(asKnowledgeMapDto);

        // 将DTO转换为实体对象，以便于保存到数据库
        AsKnowledgeMap asKnowledgeMap = new AsKnowledgeMap();
        BeanUtils.copyProperties(asKnowledgeMapDto, asKnowledgeMap);

        // 保存新的知识地图条目到数据库
        asKnowledgeService.save(asKnowledgeMap);

        // 使用LambdaQueryWrapper获取所有知识地图条目
        // 考虑是否需要所有数据
        List<AsKnowledgeMap> list = asKnowledgeService.list(new LambdaQueryWrapper<AsKnowledgeMap>());

        // 将实体对象列表转换为DTO列表并返回
        return list.stream().map(knowledgeMap -> {
            AsKnowledgeMapDto dto = new AsKnowledgeMapDto();
            BeanUtils.copyProperties(knowledgeMap, dto);
            return dto;
        }).collect(Collectors.toList());
    }


    /**
     * 更新知识地图条目并返回所有条目的DTO列表
     *
     * @param asKnowledgeMapDto 要更新的知识地图条目的数据传输对象
     * @return 包含所有知识地图条目的DTO列表
     */
    @Override
    public List<AsKnowledgeMapDto> updateAsKnowledgeMap(@Valid AsKnowledgeMapDto asKnowledgeMapDto) {
        // 验证数据有效性
        validateData(asKnowledgeMapDto);

        // 将DTO转换为实体对象，以便于更新到数据库
        AsKnowledgeMap asKnowledgeMap = new AsKnowledgeMap();
        BeanUtils.copyProperties(asKnowledgeMapDto, asKnowledgeMap);

        // 更新知识地图条目到数据库
        asKnowledgeService.updateById(asKnowledgeMap);

        // 使用LambdaQueryWrapper获取所有知识地图条目
        // 考虑是否需要所有数据
        List<AsKnowledgeMap> list = asKnowledgeService.list(new LambdaQueryWrapper<AsKnowledgeMap>());

        // 将实体对象列表转换为DTO列表并返回
        return list.stream().map(knowledgeMap -> {
            AsKnowledgeMapDto dto = new AsKnowledgeMapDto();
            BeanUtils.copyProperties(knowledgeMap, dto);
            return dto;
        }).collect(Collectors.toList());
    }


    /**
     * 验证数据的有效性
     *
     * @param asKnowledgeMapDto 要验证的数据传输对象
     */
    private void validateData(AsKnowledgeMapDto asKnowledgeMapDto) {
        if (asKnowledgeMapDto.getSubject() == null || asKnowledgeMapDto.getSubject() < 0 || asKnowledgeMapDto.getSubject() > 1) {
            throw new IllegalArgumentException("Invalid subject value.");
        }
        if (asKnowledgeMapDto.getLearningStage() == null || asKnowledgeMapDto.getLearningStage() < 0 || asKnowledgeMapDto.getLearningStage() > 1) {
            throw new IllegalArgumentException("Invalid learningStage value.");
        }
        if (asKnowledgeMapDto.getGrade() == null || asKnowledgeMapDto.getGrade() < 0 || asKnowledgeMapDto.getGrade() > 2) {
            throw new IllegalArgumentException("Invalid grade value.");
        }
        if (asKnowledgeMapDto.getSemester() == null || asKnowledgeMapDto.getSemester() < 0 || asKnowledgeMapDto.getSemester() > 1) {
            throw new IllegalArgumentException("Invalid semester value.");
        }
        if (!"0".equals(asKnowledgeMapDto.getLearningContent()) && !"1".equals(asKnowledgeMapDto.getLearningContent())) {
            throw new IllegalArgumentException("Invalid learningContent value.");
        }
    }

    /**
     * 查询所有符合条件的知识点映射数据
     *
     * @param asKnowledgeMapDto 查询条件封装对象
     * @return 符合条件的知识点映射数据列表
     */
    public List<AsKnowledgeMapDto> selverAll(AsKnowledgeMapDto asKnowledgeMapDto) {
        // 创建查询条件构造器
        LambdaQueryWrapper<AsKnowledgeMap> queryWrapper = new LambdaQueryWrapper<>();

        // 根据 DTO 中的字段添加查询条件
        if (asKnowledgeMapDto.getSubject() != null) {
            queryWrapper.eq(AsKnowledgeMap::getSubject, asKnowledgeMapDto.getSubject());
        }
        if (asKnowledgeMapDto.getLearningStage() != null) {
            queryWrapper.eq(AsKnowledgeMap::getLearningStage, asKnowledgeMapDto.getLearningStage());
        }
        if (asKnowledgeMapDto.getSemester() != null) {
            queryWrapper.eq(AsKnowledgeMap::getSemester, asKnowledgeMapDto.getSemester());
        }
        if (asKnowledgeMapDto.getLearningContent() != null) {
            queryWrapper.eq(AsKnowledgeMap::getLearningContent, asKnowledgeMapDto.getLearningContent());
        }
        // 其他条件可以根据需要添加

        // 执行查询
        List<AsKnowledgeMap> list = asKnowledgeService.list(queryWrapper);

        // 将查询结果转换为 DTO 列表并返回
        return list.stream().map(knowledgeMap -> {
            AsKnowledgeMapDto dto = new AsKnowledgeMapDto();
            BeanUtils.copyProperties(knowledgeMap, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    /**
     * 批量删除知识地图条目
     *
     * @param ids 要删除的知识地图条目的ID列表
     * @return 删除是否成功
     */
    @Override
    public boolean removeBatchByIds(List<Long> ids) {
        // 使用LambdaQueryWrapper来构建批量删除的条件
        LambdaQueryWrapper<AsKnowledgeMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AsKnowledgeMap::getId, ids);
        // 执行批量删除操作
        return asKnowledgeService.remove(queryWrapper);
    }

}
